﻿using CodeGenerator.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CodeGenerator.Dialog
{
    public partial class UpdateDialog : Form
    {
        IEnumerable<dynamic> UpdateLogs { get; set; } = new List<dynamic>();
        public UpdateDialog(List<AppUpdateInfo> data)
        {
            InitializeComponent();
            this.Text = $"程序更新 当前版本：V{Main.VersionStr}";
            UpdateLogs = data;
            ShowAppUpdateLog();
        }
        private void ShowAppUpdateLog()
        {
            var data = UpdateLogs as List<AppUpdateInfo>;
            foreach (var log in data)
            {

                var vStr = log.Version;
                vStr = vStr.Contains(".") ? vStr : $"{vStr}.0";
                var title = $"更新日期：{log.Date}   版本：V{vStr} \n";
                var desc = $"更新内容：\n{log.Desc}\n\n";
                logRichText.Text += title;
                logRichText.Text += desc;
            }
        }

        private async void UpdateBtnClicked(object sender, EventArgs e)
        {
            if (updateBtn.Text == "重启应用")
            {
                Environment.Exit(0);
                return;
            }
            updateBtn.Loading = true;
            updateBtn.Text = "下载中";
            cancelBtn.Visible = false;
            await Task.Delay(300);
            try
            {
                if (UpdateLogs is List<AppUpdateInfo>)
                {
                    var data = UpdateLogs as List<AppUpdateInfo>;
                    var lastest = data[0];
                    string url = lastest.Url;
                    var fileName = $"app_v{lastest.Version}";
                    FileUtil.DeleteDirectory(CommonUtil.GenFullPath("tmp"));
                    var path = CommonUtil.GenFullPath($"tmp/{fileName}.zip");
                    await Task.Run(() =>
                    {
                        UpdateUtil.DownloadFile(url, path);
                    });
                    updateBtn.Text = "解压中";
                    await Task.Delay(1000);
                    await Task.Run(() =>
                    {
                        ZipFile.ExtractToDirectory(path, CommonUtil.GenFullPath("tmp/update_app"));
                    });
                    updateBtn.Text = "重启应用";
                    updateBtn.Loading = false;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (updateBtn.Text != "重启应用")
                {
                    cancelBtn.Visible = true;
                    updateBtn.Loading = false;
                    updateBtn.Text = "更新";
                }
            }
        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
    public class AppUpdateInfo
    {
        public string Version { get; set; }
        public string Url { get; set; }
        public string Desc { get; set; }
        public string Date { get; set; }
    }
}
